package com.ihome.android.academy;

import com.ihome.android.EAActivity;
import com.ihome.android.annotation.EAInjectView;
import com.ihome.android.common.ConfigUtil;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.academy.R;
import com.ihome.android.market2.asynctask.GetSpecTopicUrlTask;
import com.ihome.android.market2.asynctask.GetViewAppDetailTask;
import com.ihome.android.market2.view.BasicWebview;
import com.ihome.android.util.AppUtils;
import com.ihome.android.util.log.LogUtil;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;

public class WebViewActivity extends EAActivity {
	private String				TAG					= "WebViewActivity";
	/** Message **/
	public final static int		MSG_APP_DETAIL		= 0x01;
	public final static int		MSG_REV_TIMEOUT		= 0x02;
	public final static int		MSG_ERROR_CLIENT	= 0x03;
	public final static int		MSG_ERROR_SERVER	= 0x04;

	@EAInjectView(id = R.id.bsview)
	private BasicWebview		webView;
	private String				specUrl				= null;
	private int					sectionId			= 0;
	
	private String              pageUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web_view);

		pageUrl = getIntent().getExtras().getString(HttpKeys.KEY_APPURL);
		int flag = getIntent().getExtras().getInt(HttpKeys.KEY_FLAG);
		if(flag==1){
			webView.addJavascriptInterface(new JSInterface(this, mHandler), "android");
		}
//		getSpecTopicUrl(sectionId);
		loadSpecUrl(pageUrl);
	}

	public void loadSpecUrl(String url) {
		LogUtil.d(TAG, "url = " + url);
		specUrl = url;
		if (specUrl == null || specUrl.trim().equals("")) {
			return;
		}
		webView.loadUrl(specUrl);
	}

	public Handler	mHandler	= new Handler() {
									public void handleMessage(Message msg) {
										switch (msg.what) {
										case MSG_APP_DETAIL:
											LogUtil.d(TAG, "to app detail");

											break;
										default:
											break;
										}
									}
								};

	public class JSInterface {
		private Context	mContext;
		private Handler	hander	= null;

		public JSInterface(Context c, Handler hander) {
			mContext = c;
			this.hander = hander;
		}

		@JavascriptInterface
		public void appDetail(int no, String tag, String appID) {
			LogUtil.d(TAG, "appDetail");
			ToAppDetail(appID);
		}

		private void ToAppDetail(String appID) {
			if (AppUtils.isPkgExist(WebViewActivity.this, "com.ihome.android.market2")) {
				Intent intent = new Intent("com.ihome.android.market2.DetailActivity");
				intent.addCategory(Intent.CATEGORY_DEFAULT);
				intent.putExtra(HttpKeys.KEY_APPID, Integer.parseInt(appID));//测试数据,"765"
				startActivity(intent);
			}

		}
	}
	
}
